package com.baiqian.doupo.item;

import com.baiqian.doupo.Utils;
import com.baiqian.doupo.block.BlockRegistry;
import com.baiqian.doupo.effect.EffectRegistry;
import com.baiqian.doupo.modgroup.DouPoModGroup;
import net.minecraft.client.util.ITooltipFlag;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Food;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.potion.EffectInstance;
import net.minecraft.stats.Stats;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Direction;
import net.minecraft.util.Hand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.BlockRayTraceResult;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraft.world.World;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.List;

public class DanYao extends Item{
//    protected double quality=100;//丹药品质[0,100]
    public DanYao(Properties properties) {
        super(properties);
    }

    public static ItemStack getDanYao(List<ItemStack> list,double quality){
        //聚气散:两个紫叶兰草+一个洗骨花
        if(list.size()==3){
            int zi_ye_lan_cao_num=0;
            int xi_gu_hua_num=0;
            for(ItemStack itemStack:list){
                if(ZiYeLanCao.class.equals(itemStack.getItem().getClass())){
                    ++zi_ye_lan_cao_num;
                }else if(XiGuHua.class.equals(itemStack.getItem().getClass())){
                    ++xi_gu_hua_num;
                }
            }
            if(zi_ye_lan_cao_num==2&&xi_gu_hua_num==1){
                ItemStack itemStack = new ItemStack(ItemRegistry.ju_qi_san.get());
                ((JuQiSan)itemStack.getItem()).setQuality(quality,itemStack);
                itemStack.setCount((int) (quality/20+1));
                return itemStack;
            }
        }
        //筑基灵液:三个紫叶兰草+2个洗骨花+一个木系一级魔核
        if(list.size()==6){
            int zi_ye_lan_cao_num=0;
            int xi_gu_hua_num=0;
            int mu_xi_yi_ji_num=0;
            for(ItemStack itemStack:list){
                if(ZiYeLanCao.class.equals(itemStack.getItem().getClass())){
                    ++zi_ye_lan_cao_num;
                }else if(XiGuHua.class.equals(itemStack.getItem().getClass())){
                    ++xi_gu_hua_num;
                }else if(itemStack.getItem() instanceof MoHe){
                    MoHe moHe = (MoHe) itemStack.getItem();
                    if(moHe.getAttr()==3&&moHe.getLevel()==1)
                    ++mu_xi_yi_ji_num;
                }
            }
            if(zi_ye_lan_cao_num==3&&xi_gu_hua_num==2&&mu_xi_yi_ji_num==1){
                ItemStack itemStack = new ItemStack(ItemRegistry.zhu_ji_ling_ye.get());
                ((ZhuJiLingYe)itemStack.getItem()).setQuality(quality,itemStack);
                itemStack.setCount(1);
                return itemStack;
            }
        }
        return null;
    }

//    @Override
//    public boolean updateItemStackNBT(CompoundNBT nbt) {
//        if (nbt != null) {
//            quality = nbt.getCompound("tag").getDouble("quality");
//            return true;
//        }else{
//            return false;
//        }
//    }


    /**获取丹药品质*/
    public double getQuality(ItemStack itemStack){
        CompoundNBT compoundNBT = itemStack.getOrCreateTag();
        return compoundNBT.getDouble("quality");
    }

    /**设置丹药品质*/
    public void setQuality(double quality,ItemStack itemStack){
//        this.quality=quality;
        CompoundNBT compoundNBT = itemStack.getOrCreateTag();
        compoundNBT.putDouble("quality" ,quality);
    }

    @OnlyIn(Dist.CLIENT)
    public void addInformation(ItemStack stack, @Nullable World worldIn, List<ITextComponent> tooltip, ITooltipFlag flagIn) {
        String color = "§";
        double quality = getQuality(stack);
        if(quality>=100){
            color+="c";
        }else if(quality>=90){
            color+="6";
        }else if(quality>=60){
            color+="9";
        }else if(quality>=20){
            color+="a";
        }else{
            color+="7";
        }
        tooltip.add(new TranslationTextComponent(this.getTranslationKey() + ".1"));
        tooltip.add(ITextComponent.getTextComponentOrEmpty(new TranslationTextComponent("item." + Utils.MOD_ID +".dan_yao.quality").getString()+String.format("%s%.0f",color,quality)));
    }

}

/**聚气散*/
class JuQiSan extends DanYao {
    private static final Food food = (new Food.Builder()).build();
    public JuQiSan() {
        super(new Properties().food(food).group(DouPoModGroup.douPoDanYaoGroup));
    }

    @Override
    public ItemStack onItemUseFinish(ItemStack stack, World worldIn, LivingEntity entityLiving) {
        entityLiving.addPotionEffect(new EffectInstance(EffectRegistry.ju_qi_san_effect.get(), (int) ((30 + 1.2 * getQuality(stack)) * 20), 0));
        return super.onItemUseFinish(stack, worldIn, entityLiving);
    }
}

/**筑基灵液*/
class ZhuJiLingYe extends DanYao {
    public ZhuJiLingYe() {
        super(new Properties().group(DouPoModGroup.douPoDanYaoGroup));
    }
    @Nonnull
    @Override
    public ActionResult<ItemStack> onItemRightClick(World worldIn, PlayerEntity playerIn, Hand handIn) {
        ItemStack itemstack = playerIn.getHeldItem(handIn);
        if (!worldIn.isRemote) {
//            SnowballEntity snowballentity = new SnowballEntity(worldIn, playerIn);
//            snowballentity.setItem(itemstack);
//            snowballentity.setDirectionAndMovement(playerIn, playerIn.rotationPitch, playerIn.rotationYaw, 0.0F, 1.5F, 1.0F);
//            worldIn.addEntity(snowballentity);
            BlockRayTraceResult ray_trace_result= (BlockRayTraceResult)playerIn.pick(3,0,false);
            BlockPos blockPos = new BlockPos(ray_trace_result.getHitVec());
            BlockPos blockpos = ray_trace_result.getPos();
            Direction direction = ray_trace_result.getFace();
            BlockPos blockpos1 = blockpos.offset(direction);
            if (worldIn.isBlockModifiable(playerIn, blockpos) && playerIn.canPlayerEdit(blockpos1, direction, itemstack)) {
                worldIn.setBlockState(blockPos, BlockRegistry.zhu_ji_ling_ye.get().getDefaultState());
            }
        }

        playerIn.addStat(Stats.ITEM_USED.get(this));
        if (!playerIn.abilities.isCreativeMode) {
            itemstack.shrink(1);
        }

        return ActionResult.func_233538_a_(itemstack, worldIn.isRemote());
    }
}